package com.newtribe.security.jaas;

import javax.security.auth.login.*;
import javax.security.auth.*;
import java.security.Principal;
import javax.security.auth.callback.*;
import java.util.* ;
import javax.security.auth.spi.LoginModule;
import java.io.IOException;
import java.security.Principal;
import com.sun.security.auth.module.JndiLoginModule;

/**
 * <p> </p>
 * <p>Description: Innovatation method</p>
 * <p>Copyright: Copyright (c)  newtribe innovation 2005</p>
 * <p> </p>
 * @author not attributable
 * @version 1.0
 */

public class PasswordModal implements LoginModule {
  private Subject subject;
  private Map options =new HashMap() ;
  private CallbackHandler callbackHandler;
  private Map sharedState;
  private Object callbacks;
  public PasswordModal() {
  }


  public static void main(String[] args) throws Exception  {
  System.setProperty("java.security.auth.login.config" ,"login.txt") ;

    ConsoleCallbackHandler cbh = new ConsoleCallbackHandler();

    LoginContext s = new LoginContext("check",cbh);

    s.login();


    //s.login();
    Subject subject=s.getSubject() ;


    System.out.println(subject);



  }

  public void initialize(Subject subject, CallbackHandler
                         callbackHandler,

                         Map sharedState, Map options) {
    this.subject = subject;
    this.callbackHandler = callbackHandler;
    this.sharedState = sharedState;
    this.options = options;

    String url = (String) options.get("url");
    String driverClass = (String) options.get("driver");
    boolean  debug = "true".equalsIgnoreCase( (String) options.get("debug"));
  }
  private boolean check(String a ,String b) {
    this.subject.getPrincipals().add(null) ;
    return true ;
  }
  public boolean login() throws LoginException  {
    if (callbackHandler == null)
      throw new LoginException("no handler");

    NameCallback nameCb = new NameCallback("user: ");
    PasswordCallback passCb = new PasswordCallback("password: ", true);
    TextInputCallback c =new TextInputCallback("s") ;

    Callback [] callbacks = new Callback[] {
        c};
    try {
      callbackHandler.handle(callbacks);
    }
    catch (UnsupportedCallbackException ex) {
    }
    catch (IOException ex) {
    }

    String username = nameCb.getName();
    String password = null;// passCb.getPassword();
    boolean success = check(username, password);
    if (true) throw  new LoginException("dfd") ;

    return (false);
  }

public boolean commit() throws LoginException{
  return true ;
}
public boolean abort() throws LoginException{
 return true ;
}

  public boolean logout() throws LoginException{
   return true ;
 }


 static class ConsoleCallbackHandler implements CallbackHandler {

      public void handle(Callback[] callbackArray) throws IOException, UnsupportedCallbackException{
        System.out.println("call back" );

      }

  }



}
